package com.java.base.exercise;

import cn.hutool.core.collection.CollectionUtil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.Callable;

/**
 * @Author RenPu
 * @Date 2021/12/6 15:50
 * @Version 1.0
 * @Description:  ThreadLocal避免内存泄露的写法
 **/
public class Test006 {

    private static ThreadLocal<Integer> threadLocal = new ThreadLocal<Integer>();


    public static void main(String[] args) {

        int ciyd = getCIYD(20);
        System.out.println(ciyd);


        List<String> list = new ArrayList<>();

        list.add("1");
        list.add("2");
        list.add("3");
        list.add("4");
        list.add("5");
        Collections.shuffle(list);
        System.out.println(list);







    }


    public static int getC(int count) {

        if (Objects.isNull(threadLocal.get())) {
            threadLocal.set(1000);
        }
        return threadLocal.get();
    }


    public static int getCIYD(int count) {
        for (int i = 0; i < 10; i++) {
            Callable<Integer> callable = new Callable<Integer>() {
                @Override
                public Integer call() throws Exception {
                    // System.out.println("当前线程:>>>>> ".concat(Thread.currentThread().getId()+""));
                    try {
                        return getC(20);
                    } finally {
                        threadLocal.remove();
                    }
//                    return getD(null);
                }
            };

        }
        return count;
    }




}



